package org.eclipse.wb.swt;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

public class ResourceManageX {
	private static Map<String, Image> m_imageMap = new HashMap<String, Image>();

	public static Image createImage(String path, int width, int height) {
		Image image;
		try {
			image = getImage(new FileInputStream(path));
			image = new Image(image.getDevice(), image.getImageData().scaledTo(width, height));
			return image;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getColorImage(0xffffff, width, height);
	}

	public static Image getImage(String path) {
		try {
			return getImage(new FileInputStream(path));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Image getImage(String path, int width, int height) {
		Image image = m_imageMap.get(path);
		if (image == null) {
			try {
				image = getImage(new FileInputStream(path));
				image = new Image(image.getDevice(), image.getImageData().scaledTo(width, height));
				m_imageMap.put(path, image);
			} catch (Exception e) {
				image = getMissingImage();
				m_imageMap.put(path, image);
			}
		}
		return image;
	}

	public static Image loadImage(String path, int dw, int dh) {
		return loadImage(path, dw, dh, true);
	}

	public static Image loadImage(String path, int dw, int dh, boolean reNull) {
		try {
			Image image = getImage(new FileInputStream(path));
			image = new Image(image.getDevice(), image.getImageData().scaledTo(dw, dh));
			return image;
		} catch (Exception e) {
			if (reNull) {
				return null;
			} else {
				return getMissingImage(dw, dh);
			}
		}

	}

	public static Image getImage(int color, int width, int height) {
		return getColorImage(color, width, height);
	}

	protected static Image getImage(InputStream stream) throws IOException {
		try {
			Display display = Display.getCurrent();
			ImageData data = new ImageData(stream);
			if (data.transparentPixel > 0) {
				return new Image(display, data, data.getTransparencyMask());
			}
			return new Image(display, data);
		} finally {
			stream.close();
		}
	}

	private static final int MISSING_IMAGE_SIZE = 10;

	public static Color getColor(int rgbColor) {
		return new Color(null, (rgbColor & 0xff0000) >> 16, (rgbColor & 0x00ff00) >> 8, rgbColor & 0x0000ff);
	}

	public static Color getColor(RGB rgb) {
		return new Color(null, rgb);
	}

	public static int getRGB(RGB rgb) {
		return (rgb.red << 16) | (rgb.green << 8) | (rgb.blue);
	}

	private static Image getMissingImage() {
		Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
		//
		GC gc = new GC(image);
		gc.setBackground(getColor(SWT.COLOR_RED));
		gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
		gc.dispose();
		//
		return image;
	}

	private static Image getMissingImage(int tx, int ty) {
		Image image = new Image(Display.getCurrent(), tx, ty);
		//
		GC gc = new GC(image);
		gc.setBackground(getColor(SWT.COLOR_RED));
		gc.fillRectangle(0, 0, tx, ty);
		gc.dispose();
		//
		return image;
	}

	private static Image getColorImage(int color, int width, int height) {
		Color dColor = new Color(null, (color & 0xff0000) >> 16, (color & 0x00ff00) >> 8, color & 0x0000ff);
		Image image = new Image(Display.getCurrent(), width, height);
		//
		GC gc = new GC(image);
		gc.setBackground(dColor);
		gc.fillRectangle(0, 0, width, height);
		gc.dispose();
		//
		return image;
	}

	public static Image getImage(Class<?> clazz, String path) {
		String key = clazz.getName() + '|' + path;
		Image image = m_imageMap.get(key);
		if (image == null) {
			try {
				image = getImage(clazz.getResourceAsStream(path));
				m_imageMap.put(key, image);
			} catch (Exception e) {
				image = getMissingImage();
				m_imageMap.put(key, image);
			}
		}
		return image;
	}

	public static Image getImage(Class<?> clazz, String path, int width, int height) {
		Image image;
		try {
			image = getImage(clazz.getResourceAsStream(path));
			image = new Image(image.getDevice(), image.getImageData().scaledTo(width, height));
		} catch (IOException e) {
			image = getMissingImage(width, height);
		}
		return image;
	}

}
